//
//  DKAuthLoanDetailCell.m
//  DanKU
//
//  Created by 吴斌 on 2023/12/7.
//

#import "DKAuthLoanDetailCell.h"
#import "DKHomeLoanDetailView.h"
static NSString * DKAuthLoanDetailCellId = @"DKAuthLoanDetailCellId";
@interface DKAuthLoanDetailCell()<TTTAttributedLabelDelegate>
@property (nonatomic, strong)UILabel * dk_AuthLoanDetailAmountLab;
@property (nonatomic,strong) DKHomeLoanDetailView *dk_HomeLoanDetailView;
@property (nonatomic,strong) UIButton *dk_LoanAgreementBtn;
@property (nonatomic,strong) UIButton *dk_LoanApplyBtn;
@end
@implementation DKAuthLoanDetailCell
+ (instancetype)authLoanDetailCell:(UITableView *)tableView{
    DKAuthLoanDetailCell * dk_AuthLoanDetailCell = [tableView dequeueReusableCellWithIdentifier:DKAuthLoanDetailCellId];
    if (!dk_AuthLoanDetailCell) {
        dk_AuthLoanDetailCell = [[DKAuthLoanDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DKAuthLoanDetailCellId];
    }
    dk_AuthLoanDetailCell.backgroundColor = [UIColor clearColor];
    dk_AuthLoanDetailCell.selectionStyle = UITableViewCellSelectionStyleNone;
    dk_AuthLoanDetailCell.accessoryType = UITableViewCellAccessoryNone;
    return dk_AuthLoanDetailCell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self dk_addSubView];
    }
    return self;
}
- (void)dk_addSubView{
    UILabel * dk_AuthLoanHeadLab = [[UILabel alloc] init];
    dk_AuthLoanHeadLab.textColor = DK_Hex(@"#333333");
    dk_AuthLoanHeadLab.text = @"Loan details";
    dk_AuthLoanHeadLab.font = DK_FONT_Medium(17);
    [self.contentView addSubview:dk_AuthLoanHeadLab];
    [dk_AuthLoanHeadLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.top.equalTo(@(DK_IsWidthScale(63)+DK_SafeAreaTop));
        make.height.equalTo(@(DK_IsWidthScale(24)));
    }];
    UIView *dk_AuthLoanBgView = [[UIView alloc] init];
    dk_AuthLoanBgView.backgroundColor = UIColor.whiteColor;
    dk_AuthLoanBgView.layer.cornerRadius = DK_IsWidthScale(50);
    dk_AuthLoanBgView.clipsToBounds = YES;
    [self.contentView addSubview:dk_AuthLoanBgView];
    [dk_AuthLoanBgView  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(0)));
        make.right.equalTo(@(-DK_IsWidthScale(0)));
        make.top.equalTo(dk_AuthLoanHeadLab.mas_bottom).offset(DK_IsWidthScale(20));
        make.bottom.equalTo(@(-DK_IsWidthScale(0)));
    }];
    
    UILabel * dk_AuthLoanDetailHeadLab = [[UILabel alloc] init];
    dk_AuthLoanDetailHeadLab.textColor = DK_Hex(@"#333333");
    dk_AuthLoanDetailHeadLab.font = DK_FONT_Regular(12);
    dk_AuthLoanDetailHeadLab.text = @"Loan amount";
    dk_AuthLoanDetailHeadLab.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:dk_AuthLoanDetailHeadLab];
    [dk_AuthLoanDetailHeadLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@(DK_IsWidthScale(0)));
        make.top.equalTo(dk_AuthLoanHeadLab.mas_bottom).offset(DK_IsWidthScale(40));
        make.height.equalTo(@(DK_IsWidthScale(17)));
    }];
    self.dk_AuthLoanDetailAmountLab = [[UILabel alloc] init];
    self.dk_AuthLoanDetailAmountLab.textColor = DK_Hex(@"#333333");
    self.dk_AuthLoanDetailAmountLab.font = DK_FONT_Regular(17);
    self.dk_AuthLoanDetailAmountLab.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:self.dk_AuthLoanDetailAmountLab];
    [self.dk_AuthLoanDetailAmountLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@(DK_IsWidthScale(0)));
        make.height.equalTo(@(DK_IsWidthScale(20)));
        make.top.equalTo(dk_AuthLoanDetailHeadLab.mas_bottom).offset(DK_IsWidthScale(20));
    }];
    self.dk_HomeLoanDetailView = [[DKHomeLoanDetailView alloc] init];
    [self.contentView addSubview:self.dk_HomeLoanDetailView];
    [self.dk_HomeLoanDetailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@(DK_IsWidthScale(0)));
        make.top.equalTo(self.dk_AuthLoanDetailAmountLab.mas_bottom).offset(DK_IsWidthScale(10));
        make.height.equalTo(@(DK_IsWidthScale(0)));
    }];
    //协议
    self.dk_LoanAgreementBtn = [[UIButton alloc] init];
    [self.dk_LoanAgreementBtn setBackgroundImage:[UIImage imageNamed:@"dk_register_noSelect"] forState:UIControlStateNormal];
    [self.dk_LoanAgreementBtn setBackgroundImage:[UIImage imageNamed:@"dk_register_Select"] forState:UIControlStateSelected];
    [self.dk_LoanAgreementBtn addTarget:self action:@selector(goAgreeAgreenment) forControlEvents:UIControlEventTouchUpInside];
    self.dk_LoanAgreementBtn.selected = YES;
    [self.contentView addSubview:self.dk_LoanAgreementBtn];
    [self.dk_LoanAgreementBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(45)));
        make.width.equalTo(@(DK_IsWidthScale(16)));
        make.height.equalTo(@(DK_IsWidthScale(16)));
        make.top.equalTo(self.dk_HomeLoanDetailView.mas_bottom).offset(DK_IsWidthScale(61));
    }];
    TTTAttributedLabel * dk_LoanAgreementLab = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
    dk_LoanAgreementLab.numberOfLines = 0;
    [self.contentView addSubview:dk_LoanAgreementLab];
    [dk_LoanAgreementLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dk_LoanAgreementBtn.mas_right).offset(DK_IsWidthScale(6));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.height.equalTo(@(DK_IsWidthScale(17)));
        make.centerY.equalTo(self.dk_LoanAgreementBtn);
    }];
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:@"I have read and agreed to《 Loan Agreement》"];
    [attStr addAttribute:NSFontAttributeName value:DK_FONT_Regular(12) range:NSMakeRange(0,attStr.length)];
    [attStr addAttribute:NSForegroundColorAttributeName value:DK_Hex(@"#333333") range:NSMakeRange(0,attStr.length)];
    [attStr addAttribute:NSForegroundColorAttributeName value:DK_Hex(@"#4566FF") range:NSMakeRange(25,17)];
    dk_LoanAgreementLab.delegate = self;
    dk_LoanAgreementLab.attributedText = attStr;
    dk_LoanAgreementLab.enabledTextCheckingTypes = NSTextCheckingTypeLink;
    dk_LoanAgreementLab.linkAttributes = @{NSForegroundColorAttributeName:DK_Hex(@"#4566FF")};
    [dk_LoanAgreementLab addLinkToURL:[NSURL URLWithString:DK_LoanAgreement] withRange:NSMakeRange(25,17)];
    //next
    self.dk_LoanApplyBtn = [[UIButton alloc] init];
    [self.dk_LoanApplyBtn setTitle:@"Apply now" forState:UIControlStateNormal];
    [self.dk_LoanApplyBtn setTitleColor:DK_Hex(@"#FFFFFF") forState:UIControlStateNormal];
    [self.dk_LoanApplyBtn setTitleColor:DK_Hex(@"#4566FF") forState:UIControlStateDisabled];
    self.dk_LoanApplyBtn.titleLabel.font = DK_FONT_Semibold(16);
    [self.dk_LoanApplyBtn setBackgroundImage:DK_GetImage(@"dk_registerNextSelectBg") forState:UIControlStateNormal];
    [self.dk_LoanApplyBtn setBackgroundImage:DK_GetImage(@"dk_registerNextBg") forState:UIControlStateDisabled];
    [self.dk_LoanApplyBtn addTarget:self action:@selector(goLoanApply) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.dk_LoanApplyBtn];
    [self.dk_LoanApplyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.height.equalTo(@(DK_IsWidthScale(50)));
        make.top.equalTo(dk_LoanAgreementLab.mas_bottom).offset(DK_IsWidthScale(20));
    }];
    UIButton * dk_LoanApplyAgreementBtn = [[UIButton alloc] init];
    [dk_LoanApplyAgreementBtn setTitle:@"《Privacy Agreement》" forState:UIControlStateNormal];
    [dk_LoanApplyAgreementBtn setTitleColor:DK_Hex(@"#CCCCCC") forState:UIControlStateNormal];
    dk_LoanApplyAgreementBtn.titleLabel.font = DK_FONT_Regular(12);
    [dk_LoanApplyAgreementBtn addTarget:self action:@selector(CheckAgreement) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:dk_LoanApplyAgreementBtn];
    [dk_LoanApplyAgreementBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.top.equalTo(self.dk_LoanApplyBtn.mas_bottom).offset(DK_IsWidthScale(20));
//        make.bottom.equalTo(@(-DK_IsWidthScale(100)-DK_SafeAreaBottom));
        make.height.equalTo(@(DK_IsWidthScale(17)));
    }];
}
-(void)setDk_LoanDetailMdl:(DKLoanDetailModel *)dk_LoanDetailMdl{
    _dk_LoanDetailMdl = dk_LoanDetailMdl;
    if(_dk_LoanDetailMdl){
        self.dk_AuthLoanDetailAmountLab.text = [NSString stringWithFormat:@"₹%@",[NSString makeShowMoney:_dk_LoanDetailMdl.loanAmount]];
        self.dk_HomeLoanDetailView.dk_LoanDetailMdl = _dk_LoanDetailMdl;
        CGFloat allHeight = 0;
        for (DKLoanDetailPairsModel *dk_LoanDetailPairsMdl in _dk_LoanDetailMdl.pairs) {
            CGFloat keyWidth = [NSString getContentSize:dk_LoanDetailPairsMdl.key withFont:DK_FONT_Regular(17) withSize:CGSizeMake(CGFLOAT_MAX, DK_IsWidthScale(40))].width;
            CGFloat valueHeight = [NSString getContentSize:dk_LoanDetailPairsMdl.value withFont:DK_FONT_Regular(17) withSize:CGSizeMake(DK_SCREEN_W-DK_IsWidthScale(40)-keyWidth, CGFLOAT_MAX)].height;
            if((valueHeight < DK_IsWidthScale(40))){
                allHeight += DK_IsWidthScale(40);
            }else{
                allHeight += valueHeight+DK_IsWidthScale(10);
            }
        }
        [self.dk_HomeLoanDetailView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(allHeight));
        }];
    }
}
- (void)goAgreeAgreenment{
    self.dk_LoanAgreementBtn.selected = !self.dk_LoanAgreementBtn.isSelected;
    self.dk_LoanApplyBtn.enabled = self.dk_LoanAgreementBtn.isSelected;
}
- (void)goLoanApply{
    if(self.GoLoanApplyBlock){
        self.GoLoanApplyBlock();
    }
}
- (void)CheckAgreement{
    if(self.GoPrivacyAgreementBlock){
        self.GoPrivacyAgreementBlock();
    }
}
#pragma mark -- TTTAttributedLabelDelegate
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url{
    if(self.GoLoanAgreementBlock){
        self.GoLoanAgreementBlock();
    }
}
@end
